package com.xiguaji.tiktok.v1.ins.media.controller;

import com.xiguaji.boot.toolkit.jdbc.Page;
import com.xiguaji.tiktok.config.exception.ParamsException;
import com.xiguaji.tiktok.utils.Ret;
import com.xiguaji.tiktok.v1.InsBaseController;
import com.xiguaji.tiktok.v1.ins.favorite.dto.FavoriteTypeEnum;
import com.xiguaji.tiktok.v1.ins.favorite.model.InsFavorite;
import com.xiguaji.tiktok.v1.ins.favorite.service.InsFavoriteService;
import com.xiguaji.tiktok.v1.ins.media.dto.InsMediaData;
import com.xiguaji.tiktok.v1.ins.media.dto.InsMediaDetail;
import com.xiguaji.tiktok.v1.ins.media.dto.InsMediaSearchRequest;
import com.xiguaji.tiktok.v1.ins.media.service.InsMediaService;
import com.xiguaji.tiktok.v1.tiktok.permission.Limit;
import com.xiguaji.tiktok.v1.tiktok.permission.RequestLimit;
import com.xiguaji.tiktok.v1.user.log.Operation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@RestController
@Api(value = "ins帖子",tags = {"ins帖子"})
public class InsMediaController extends InsBaseController {

    @Autowired
    InsMediaService insMediaService;

    @Autowired
    InsFavoriteService insFavoriteService;

    @ApiOperation(value = "获取INS帖子列表")
    @RequestMapping(value = "media",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @RequestLimit(value = "InsMediaSearchPageLimit",property = "page,pageSize",validateType = Limit.VIEW_DATA_LIMIT)
    @Operation(value = "获取INS帖子列表")
    public String getMediaPage(String data) throws UnsupportedEncodingException, ParamsException {
        InsMediaSearchRequest insMediaSearchRequest = getParam(InsMediaSearchRequest.class, URLDecoder.decode(data,"UTF-8"));
        Page<InsMediaData> insMediaPage = insMediaService.findByPage(insMediaSearchRequest);

        insMediaPage.getList().forEach( media ->{
            InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_MEDIA.getCode(),media.getId(),(int)getUser().getId());

            if(null != insFavorite) {
                media.setCollectId(insFavorite.getId());
            }
        });

        return Ret.success("SUCCESS",insMediaPage);
    }


    @ApiOperation(value = "获取INS帖子列表")
    @RequestMapping(value = "media/search",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "获取INS帖子列表")
    public String searchMediaPage(String data) throws UnsupportedEncodingException, ParamsException {
        InsMediaSearchRequest insMediaSearchRequest = getParam(InsMediaSearchRequest.class, URLDecoder.decode(data,"UTF-8"));
        if(insMediaSearchRequest.getPage()*insMediaSearchRequest.getPageSize()>10){
            return Ret.noPermission();
        }
        return Ret.success("SUCCESS",insMediaService.findByPage(insMediaSearchRequest));
    }

    @ApiOperation(value = "获取IN帖子信息")
    @RequestMapping(value = "media/{id}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "获取INS帖子信息")
    public String mediaDetail(@PathVariable long id){
        InsMediaDetail insMediaDetail = insMediaService.insMediaById(id);
        InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_MEDIA.getCode(),insMediaDetail.getId(),(int)getUser().getId());

        if(null != insFavorite) {
            insMediaDetail.setCollectId(insFavorite.getId());
        }
        return Ret.success("SUCCESS",insMediaDetail);
    }
}
